package edu.ynu.e_platform.controllers;

import edu.ynu.e_platform.common.controller.LogicController;
import edu.ynu.e_platform.common.service.LogicService;
import edu.ynu.e_platform.dao.ShopDao;
import edu.ynu.e_platform.dto.ShopDTO;
import edu.ynu.e_platform.dto.ShopItemDTO;
import edu.ynu.e_platform.entities.Item;
import edu.ynu.e_platform.entities.Shop;
import edu.ynu.e_platform.entities.ShopItem;
import edu.ynu.e_platform.service.ShopService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author manwen Yang
 */
@Slf4j
@Tag(name = "门店 实体的控制器")
@RestController
@RequestMapping("/shop")
public class ShopController
        extends LogicController<ShopService, ShopDao, Shop, Long> {

    @Resource
    ModelMapper modelMapper;

    public ShopController(@Autowired ShopService ls) {
        super(ls);
    }

    @PostMapping("/updateImg")
    @Operation(summary = "更新图片")
    public String updateImg(Long shopId, MultipartFile file){
        return getService().updateImg(shopId, file);
    }

    @Operation(summary = "门店 添加商品(默认不上架)")
    @GetMapping("/addItem")
    public ShopItemDTO addItem(Long shopId, Long itemId){

        var result = getService().addItem(shopId, itemId);

        return modelMapper.map(result, ShopItemDTO.class);
    }

    @Operation(summary = "门店 上架商品")
    @GetMapping("/putAwayItem")
    public ShopItemDTO putAwayItem(Long shopId, Long itemId){

        ShopItem results = getService().putAwayItem(shopId, itemId);
        var newResults = modelMapper.map(results, ShopItemDTO.class);

        return newResults;
    }

    @Operation(summary = "门店 下架商品")
    @GetMapping("/removeItem")
    public ShopItemDTO removeItem(Long shopId, Long itemId){

        ShopItem results = getService().removeItem(shopId, itemId);
        var newResults = modelMapper.map(results, ShopItemDTO.class);

        return newResults;
    }

    @GetMapping("/pageQueryItem")
    @Operation(summary = "分页 查询商家所有上架商品")
    public List<ShopItemDTO> pageQueryAll(Long shopId, Integer pageStart,Integer pageSize){

        Page<ShopItem> results = getService().PageQueryByItemStatus(pageStart, pageSize, shopId, 1);

        return results.stream().map(
                shopItem -> modelMapper.map(shopItem, ShopItemDTO.class)
        ).collect(Collectors.toList());
    }


}
